package com.ztman.router.common.base;

import android.app.Activity;

import java.util.HashSet;
import java.util.Set;

/**
 * @Title:ActivityCollector
 * @Package:com.ztman.router.common.base
 * @Description:Activity的对象实例收集器，用于对Activity的管理；
 * @Auther:YJH
 * @Email:yuannunhua@gmail.com
 * @Date:2018/5/149:25
 */
public class ActivityCollector {

    private static ActivityCollector activityCollector;

    //单例获取收集器对象
    public synchronized static ActivityCollector getInstance() {
        if (activityCollector == null) {
            activityCollector = new ActivityCollector();
        }
        return activityCollector;
    }

    //使用set集合盛放，无序不得重复
    private Set<Activity> allActivities;

    /**
     * 功能：压入Activity实例
     *
     * @param act
     */
    public void addActivity(Activity act) {
        if (allActivities == null) {
            allActivities = new HashSet<>();
        }
        allActivities.add(act);
    }

    /**
     * 功能：弹出Activity实例
     *
     * @param act
     */
    public void removeActivity(Activity act) {
        if (allActivities != null) {
            allActivities.remove(act);
        }
    }

    /**
     * 功能：清空集合中Activity实例，并退出程序
     */
    public void exitApp() {
        if (allActivities != null) {
            synchronized (allActivities) {
                for (Activity act : allActivities) {
                    if (allActivities.contains(act))
                        allActivities.remove(act);
                    act.finish();
                }
            }
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
}
